
import { getSwaggerApiResources, getSwaggerApiDetails } from '@/services/menu';
import { getLocalStorage } from './localStorage';

export interface menuItemType {
    name?: string;
    key?: string;
    path?: string;
    icon?: string;
    mode?: string;
    apiDetail?: any;
    children?: any;
}
export interface MenuGroupItemType {
    name?: string;
    children?: any;
}


export interface SwaggerApiResources {
    children?: SwaggerApiDetails;
    location?: string;
    name?: string;
    swaggerVersion?: string;
    url?: string;
}

export interface SwaggerApiDetails {
    basePath?: string;
    definitions?: string;
    host?: string;
    info?: string;
    paths?: string;
    swagger?: string;
    tags?: string;
}

export const getMenuData = async (key: string) => {
    const resources: SwaggerApiResources = await getSwaggerApiResources("");

    var newRes: SwaggerApiResources[] = [];
    for (var index in resources) {
        var item = resources[index];
        const response1: SwaggerApiDetails = await getSwaggerApiDetails(item.location);
        item.children = response1;
        newRes[index] = item;
    }

    const menuData = menuUtils(newRes);

    return menuData;
};

const menuUtils = (data: SwaggerApiResources[]) => {
    var menus: menuItemType[] = [];
    if (data.length > 0) {
        for (let index in data) {//需要递归啊...草，查找 routes
            var item = data[index];
            var menuItem: menuItemType = {};
            menuItem.name = item.name;
            // menuItem.key = generateUUID();
            menuItem.path = "/";
            menuItem.icon = "thunderbolt";//<MenuOutlined /><HddTwoTone /><ThunderboltTwoTone />
            menuItem.mode = "vertical";
            setItemChildren(menuItem, item);
            menus[index] = menuItem;
        }
    }
    return menus;
};
const renderMenuItemX = (sapiUrlDetailsDefinitions: any, menuItem: { children: {}[]; }, sapiUrlDetailsPaths: any, postNum: number, getNum: number) => {
    menuItem.children = getChildernMenuInfoByPaths(sapiUrlDetailsDefinitions, sapiUrlDetailsPaths, postNum, getNum, undefined);
    return menuItem;
}
const renderMenuItemY = (sapiUrlDetailsDefinitions: any, menuItem: { children: {}[]; }, sapiUrlDetailsPaths: any, postNum: number, getNum: number, sapiUrlDetailsTags: { [x: string]: { name: any; }; }) => {
    menuItem.children = [];
    var menuItemChild = [];
    var index = 0;
    for (var sapiUrlDetailsTagIndex in sapiUrlDetailsTags) {
        var groupMenu: MenuGroupItemType = {};
        var groupName = sapiUrlDetailsTags[sapiUrlDetailsTagIndex].name;
        groupMenu.name = groupName;
        groupMenu.children = getChildernMenuInfoByPaths(sapiUrlDetailsDefinitions, sapiUrlDetailsPaths, postNum, getNum, groupName);//获取childern menu信息
        menuItemChild[index] = groupMenu;
        index = index + 1;
    }
    menuItem.children = menuItemChild;
    return menuItem;
}
const getChildernMenuInfoByPaths = (sapiUrlDetailsDefinitions: any, sapiUrlDetailsPaths: { [x: string]: any; }, postNum: number, getNum: number, groupName: string | undefined) => {
    var menuItemChild = [];
    var index = 0;
    for (var apiKey in sapiUrlDetailsPaths) {
        var apiDetails = sapiUrlDetailsPaths[apiKey];
        for (var apiDetailsType in apiDetails) {
            var apiDetail = apiDetails[apiDetailsType];
            apiDetail.url = apiKey;
            apiDetail.method = apiDetailsType;
            apiDetail.sapiUrlDetailsDefinitions = sapiUrlDetailsDefinitions;
            var tagsx = apiDetail.tags[0];//接口分组
            if (groupName != undefined && groupName != 'undefined') {
                if (groupName != tagsx) {
                    continue;
                }
            }
           
            if (apiDetailsType == 'post') {
                postNum = postNum + 1;
                var menuItemPost: menuItemType = {};
                // menuItemPost.menuID = apiDetail.summary + 'post';

                menuItemPost.key = apiDetail.operationId;
                menuItemPost.name = apiDetail.summary;
                menuItemPost.path = '/swagger/apidebug/' + menuItemPost.key;
                menuItemPost.apiDetail = apiDetail;
                menuItemChild[index] = menuItemPost;
                index = index + 1;
            } else if (apiDetailsType == 'get') {
                getNum = getNum + 1;
                var menuItemGet: menuItemType = {};

                menuItemGet.key = apiDetail.operationId;
                // menuItemGet.menuID = apiDetail.summary + 'get';
                menuItemGet.name = apiDetail.summary;
                menuItemGet.path = '/swagger/apidebug/' + menuItemGet.key;
                menuItemGet.apiDetail = apiDetail;
                menuItemChild[index] = menuItemGet;
                index = index + 1;

            }
        }
    }
    function getCompareRule(name: any) {
        var n: number = 0;
        if (name != undefined) {
            var temp = name.split("-");
            if (temp.length == 1) {
                n = 0;
            }
            if (temp.length == 2) {
                var key: any = temp[0];
                if (new Number(key).toString() == "NaN") {
                    n = 0;
                } else {
                    n = new Number(key).valueOf();
                }
            }
        }
        return n;
    }
    // debugger
    function compare(obj1: menuItemType, obj2: menuItemType) {
        var name1 = obj1.name;
        var name2 = obj2.name;
        var n1: number = getCompareRule(name1);
        var n2: number = getCompareRule(name2);
        return n1 - n2;
    };
    menuItemChild.sort(compare);
    renderMenu(menuItemChild);
    return menuItemChild;
}
const renderMenu = (menuItemChild: menuItemType[]) => {
    const clearPreIndex: any = getLocalStorage("clearPreIndex");
    var claerFlag: boolean = false;
    if (clearPreIndex != undefined && clearPreIndex == 'true') {
        claerFlag = true;
    }
    const showGetOrPost: any = getLocalStorage("showGetOrPost");
    var showGetOrPostFlag: boolean = false;
    if (showGetOrPost != undefined && showGetOrPost == 'true') {
        showGetOrPostFlag = true;
    }
    for (var menuItemIndex in menuItemChild) {
        var menuItem = menuItemChild[menuItemIndex];
        var name = menuItem.name;
        if (claerFlag) {
            var nameTemp = name?.split("-");
            if (nameTemp != undefined && nameTemp.length == 2) {
                var isNaNStr = new Number(nameTemp[0]).toString();
                if (isNaNStr != 'NaN') {
                    name = nameTemp[1];
                }
            }
        }
        if (showGetOrPostFlag) {
            menuItem.name = menuItem.apiDetail.method + ":" + name;
        } else {
            menuItem.name = name;
        }
    }
}
const setItemChildren = (menuItem: any, item: SwaggerApiResources) => {
    var sapiUrlDetails: any = item.children;
    var sapiUrlDetailsTags = sapiUrlDetails.tags;//本组接口共多少分组
    var sapiUrlDetailsPaths = sapiUrlDetails.paths;
    var sapiUrlDetailsDefinitions = sapiUrlDetails.definitions;
    if (sapiUrlDetailsPaths == undefined || sapiUrlDetailsPaths == 'undefined') {
        return;
    }
    if (sapiUrlDetailsTags.length == 1) {//不分组
        menuItem = renderMenuItemX(sapiUrlDetailsDefinitions, menuItem, sapiUrlDetailsPaths, 0, 0);
    } else {
        menuItem = renderMenuItemY(sapiUrlDetailsDefinitions, menuItem, sapiUrlDetailsPaths, 0, 0, sapiUrlDetailsTags);
    }
}
